﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Source.Object_Classes;
using FrameWork.Source.Animation_Classes;
using FrameWork.Source.Texture;
using FrameWork.Source.Resource_Manager;
using Microsoft.Xna.Framework;
using Boomer.Source.TiledMap;
using Boomer.Source.Character;
using FrameWork.Source.Global_variables;

namespace Boomer.Source.Boom
{
    class CBoom : CAnimatedObject
    {

        public float timeLife { get; private set; }
        public float maxTimeLife { get; private set; }
        public int boomStr { get; set; }
        public int dame { get; set; }

        protected CFragment fragment;

        protected CTiledMap map;

        protected CCharacter user;

        public bool isExplor { get; set; }

        public Vector2 mapPos { get; private set; }

        public CBoom()
            : base()
        {
            this.Tag = FrameWork.Tag_Manager.TagManager.TAGGED.Boom;
            this.m_isAlive = false;
            timeLife = 0.0f;
            maxTimeLife = 2.0f;
            isExplor = false;
            mapPos = new Vector2();
        }


        public virtual void init()
        {
            CAnimation pAnim;
            

            pAnim = new CAnimation();
            pAnim.createAnimation(0, 0, 3, 0, 1.0f / 10.0f);

            int imageIndex;
            if (this.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.Boom)
                imageIndex = (int)CResourceManager.IMG_MAIN.BOOM;
            else
                imageIndex = (int)CResourceManager.IMG_MAIN.BOOMT;
            CTexture image = CResourceManager.getInstance().getImage(imageIndex);

            loadSprite(image, 4, 4, 46, 50, pAnim);

            fragment = new CFragment();
            fragment.init();
        }

        public bool initInfo(CCharacter user)
        {
            this.m_vec2Position = map.convertToMapPos(new Vector2( user.getBoundBox().X , user.getBoundBox().Y ));
            mapPos = map.convertToMapIndex(m_vec2Position);
            if (map.getCell((int)mapPos.X, (int)mapPos.Y) == (int)CTiledMap.MAP_TYPE.BOOM)
                return false;
            this.user = user;
            this.getCurrAnimation().m_iCurrFrame = this.getCurrAnimation().m_iStartFrame;
           
            this.dame = user.dame;
            this.boomStr = user.boomStr;

            if (this.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.Boom)
            {
                this.user.numBoom -= 1;
            }

            if (this.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.ActiveBoom)
            {
                this.user.numActiveBoom -= 1;
            }

            map.setCell((int)mapPos.X, (int)mapPos.Y, (int)CTiledMap.MAP_TYPE.BOOM);

            return true;
        }

        public void attachMap(CTiledMap map)
        {
            this.map = new CTiledMap();
            this.map = map;
        }


        public override void update(float deltaTime)
        {
            if (m_isAlive)
            {
                timeLife += deltaTime;
                if (timeLife >= maxTimeLife || isExplor)
                {
                    if (CGLOBAL.g_isPlayingSound)
                        Sound_Manager.SoundManager.getInstance().boomExplor.Play();
                    explor();
                }
                CTiledMap.updateDeep updateDeep = new CTiledMap.updateDeep(CTiledMap.updateDeepValue);

                this.m_fDeep = updateDeep(this.m_vec2Position, map);
                this.m_fDeep -= 0.01f;
                base.update(deltaTime);
            }


            fragment.update(deltaTime);
        }

        public void explor()
        {
            isExplor = false;
            timeLife = 0.0f;
            m_isAlive = false;
            if( this.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.Boom )
                this.user.numBoom += 1;
            if (this.Tag == FrameWork.Tag_Manager.TagManager.TAGGED.ActiveBoom)
                this.user.numActiveBoom += 1;
            map.setCell((int)mapPos.X, (int)mapPos.Y, (int)CTiledMap.MAP_TYPE.GRASS);
            fragment.explorsion(this.m_vec2Position, this.boomStr, this.map);
        }

        public override void draw()
        {
            fragment.draw();
            if( m_isAlive )
                base.draw();
        }

        public override bool Collision(ref CObject pObj)
        {
            return base.Collision(ref pObj);
        }
    }
}
